home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Newswatcher 2.0b22 / NW Source / Source / popuputil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  6.6 KB  |  242 lines  |  [TEXT/MMCC]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     popuputil.c
  4.  
  5.     This reusable module contains miscellaneous popup menu utility routines.
  6.     
  7.     Copyright © 1994, Northwestern University.
  8.  
  9. ----------------------------------------------------------------------------*/
  10.  
  11. #include <Packages.h>
  12.  
  13. #include "def.h"
  14. #include "popuputil.h"
  15.  
  16.  
  17.  
  18. /*----------------------------------------------------------------------------
  19.     GetPopupPString
  20.     
  21.     Get the text of an item in a Popup Menu control, as a Pascal format
  22.     string.
  23.     
  24.     Entry:    ctl = handle to popup menu control.
  25.             item = item number to get, or kCurrentPopupItem for 
  26.                 the currently selected item.
  27.                 
  28.     Exit:    str = the item text, as a Pascal format string.
  29. ----------------------------------------------------------------------------*/
  30.  
  31. void GetPopupPString (ControlHandle    ctl, short item, Str255    str)
  32. {
  33.     popupPrivateData **data;
  34.     
  35.     data = (popupPrivateData**)(**ctl).contrlData;
  36.     if (item == kCurrentPopupItem) item = GetControlValue(ctl);
  37.     GetMenuItemText((**data).mHandle, item, str);
  38. }
  39.  
  40.  
  41.  
  42. /*----------------------------------------------------------------------------
  43.     GetPopupCString
  44.     
  45.     Get the text of an item in a Popup Menu control, as a C format
  46.     string.
  47.     
  48.     Entry:    ctl = handle to popup menu control.
  49.             item = item number to get, or kCurrentPopupItem for 
  50.                 the currently selected item.
  51.                 
  52.     Exit:    str = the item text, as a C format string.
  53. ----------------------------------------------------------------------------*/
  54.  
  55. void GetPopupCString (ControlHandle ctl, short item, char *str)
  56. {
  57.     GetPopupPString(ctl, item, (StringPtr)str);
  58.     p2cstr((StringPtr)str);
  59. }
  60.  
  61.  
  62.  
  63. /*----------------------------------------------------------------------------
  64.     SetPopupItemStyle
  65.     
  66.     Set the text style for one of the items in a popup menu control.
  67.     
  68.     Entry:    ctl = handle to popup menu control.
  69.             item = item number to set, or kCurrentPopupItem for 
  70.                 the currently selected item.
  71.             style = the new text style.
  72. ----------------------------------------------------------------------------*/
  73.  
  74. void SetPopupItemStyle (ControlHandle ctl, short item, short style)
  75. {
  76.     popupPrivateData **data;
  77.     
  78.     data = (popupPrivateData**)(**ctl).contrlData;
  79.     if (item == kCurrentPopupItem) item = GetControlValue(ctl);
  80.     SetItemStyle((**data).mHandle, item, style);
  81. }
  82.  
  83.  
  84.  
  85. /*----------------------------------------------------------------------------
  86.     AddPopupItem
  87.     
  88.     Add an item to a popup menu control.
  89.     
  90.     Entry:    ctl = handle to popup menu control.
  91.             after = item number after which the new item should be
  92.                 inserted, or 0 to insert the new item and the beginning
  93.                 of the menu.
  94.             str = text or the new item, in Pascal format.
  95. ----------------------------------------------------------------------------*/
  96.  
  97. void AddPopupItem (ControlHandle ctl, short after, Str255 str)
  98. {
  99.     popupPrivateData **data;
  100.     short curValue;
  101.  
  102.     data = (popupPrivateData**)(**ctl).contrlData;
  103.     InsertMenuItem((**data).mHandle, str, after);
  104.     curValue = GetControlValue(ctl);
  105.     if (after < curValue) SetControlValue(ctl, curValue+1);
  106. }
  107.  
  108.  
  109.  
  110. /*----------------------------------------------------------------------------
  111.     DelPopupItem
  112.     
  113.     Delete an item from a popup menu control.
  114.     
  115.     Entry:    ctl = handle to popup menu control.
  116.             item = item number to delete, or kCurrentPopupItem to delete 
  117.                 the currently selected item.
  118. ----------------------------------------------------------------------------*/
  119.  
  120. void DelPopupItem (ControlHandle ctl, short item)
  121. {
  122.     popupPrivateData **data;
  123.     short curValue;
  124.  
  125.     data = (popupPrivateData**)(**ctl).contrlData;
  126.     curValue = GetControlValue(ctl);
  127.     if (item == kCurrentPopupItem) item = curValue;
  128.     DeleteMenuItem((**data).mHandle, item);
  129.     if (item < curValue) SetControlValue(ctl, curValue-1);
  130. }
  131.  
  132.  
  133. /*----------------------------------------------------------------------------
  134.     SetPopupValue
  135.     
  136.     Set the value of a popup menu control to the item matching a string.
  137.     
  138.     Entry:    ctl = handle to popup menu control.
  139.             str = Pascal format string.
  140.             isNumber = true if string and menu items are numbers.
  141.  
  142.     Exit:    function result = new control value, or 0 if no matching
  143.                 item.
  144. ----------------------------------------------------------------------------*/
  145.  
  146. short SetPopupValue (ControlHandle ctl, Str255 str, Boolean    isNumber)
  147. {
  148.     popupPrivateData **data;
  149.     long checkVal, itemVal;
  150.     MenuHandle menu;
  151.     short numItems, item;
  152.     Str255 tempStr;
  153.  
  154.     data = (popupPrivateData**)(**ctl).contrlData;
  155.     if (isNumber) StringToNum(str, &checkVal);
  156.     menu = (**data).mHandle;
  157.  
  158.     numItems = CountMItems(menu);
  159.     for (item = 1; item <= numItems; item++) {
  160.         GetMenuItemText(menu, item, tempStr);
  161.         if (isNumber) {
  162.             StringToNum(tempStr, &itemVal);
  163.             if (checkVal == itemVal) {
  164.                 SetControlValue(ctl, item);
  165.                 return item;
  166.             }
  167.         } else if (EqualString(str, tempStr, false, true)) {
  168.             SetControlValue(ctl, item);
  169.             return item;
  170.         }
  171.     }
  172.     return 0;
  173. }
  174.  
  175.  
  176.  
  177. /*----------------------------------------------------------------------------
  178.     TrackPopup
  179.     
  180.     Track a click in a typein popup menu control.
  181.     
  182.     Entry:    ctl = handle to popup menu control.
  183.             where = location of mouse click, in local coords.
  184.             checkItem = text of the item that should appear checked
  185.                 (the contents of the "partner" typein textedit field),
  186.                 in Pascal format.
  187.             isNumber = true if menu items are numbers.
  188.             
  189.     Exit:    function result = new control value, or 0 if no change.
  190.  
  191.     If the text does not match a menu item, a new item is added
  192.     at the beginning, followed by a separator line. These two extra
  193.     items are deleted before the function returns. 
  194. ----------------------------------------------------------------------------*/
  195.  
  196. short TrackPopup (ControlHandle    ctl, Point where, Str255 checkItem, Boolean    isNumber)
  197. {
  198.     popupPrivateData **data;
  199.     MenuHandle menu;
  200.     long checkVal;
  201.     short i, itemsAdded, part, newValue, oldValue;
  202.     Str255 tempStr;
  203.  
  204.     data = (popupPrivateData **)(**ctl).contrlData;
  205.     menu = (**data).mHandle;
  206.     itemsAdded = 0;
  207.     if (checkItem && *checkItem) {
  208.         oldValue = SetPopupValue(ctl, checkItem, isNumber);
  209.         if (oldValue == 0) {
  210.             if (isNumber) {
  211.                 StringToNum(checkItem, &checkVal);
  212.                 NumToString(checkVal, tempStr);
  213.                 InsertMenuItem(menu, tempStr, 0);
  214.             } else {
  215.                 InsertMenuItem(menu, checkItem, 0);
  216.             }
  217.             InsertMenuItem(menu, "\p(-", 1);
  218.             oldValue = 1;
  219.             itemsAdded = 2;
  220.             SetControlValue(ctl, oldValue);
  221.         }
  222.     } else {
  223.         oldValue = 0;
  224.     }
  225.  
  226.     part = TrackControl(ctl, where, (ControlActionUPP)-1);
  227.     newValue = GetControlValue(ctl);
  228.  
  229.     if (part != 0 && oldValue != newValue) {
  230.         newValue = newValue - itemsAdded;
  231.     } else {
  232.         newValue = 0;
  233.     }
  234.     if (itemsAdded) {
  235.         for (i = 0; i < itemsAdded; i++) {
  236.             DeleteMenuItem(menu, 1);
  237.         }
  238.         SetControlValue(ctl, newValue);
  239.     }
  240.     return newValue;
  241. }
  242.